package com.qifenqian.turbot.module.ui.marketing

import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.blankj.rxbus.RxBus
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.module.adapter.marketing.DiscountDishEditAdapter
import com.qifenqian.turbot.module.contract.MarketingContract
import com.qifenqian.turbot.module.entity.response.dish.DishEntity
import com.qifenqian.turbot.module.presenter.MarketingPresenter
import com.qifenqian.turbot.module.ui.dish.ACTION_DISCOUNT_DISH_CHOOSE
import com.qifenqian.turbot.module.ui.dish.DishManageActivity
import com.qifenqian.turbot.module.ui.dish.RX_DISH
import kotlinx.android.synthetic.main.activity_discount_dish_edit.*
import java.text.SimpleDateFormat

/**
 * @author H
 * @createDate 201/3 17:41
 * @description 添加huo或编辑单品折扣界面
 * @see
 * @since
 */
private const val PARAM1 = "action"
private const val PARAM2 = "dish"
const val ACTION_DISCOUNT_ADD = "action_add"
const val ACTION_DISCOUNT_EDIT = "action_edit"

const val EDIT_DISCOUNT_TYPE1 = 0
const val EDIT_DISCOUNT_TYPE2 = 1
const val EDIT_DISCOUNT_TYPE3 = 2

class DiscountDishEditActivity : BaseActivity<MarketingPresenter, MarketingContract.View>(), MarketingContract.View {

    companion object {
        fun startThis(context: Context, action: String, dish: String? = null) {
            val intent = Intent(context, DiscountDishEditActivity::class.java)
            intent.putExtra(PARAM1, action)
            intent.putExtra(PARAM2, dish)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_discount_dish_edit
    }

    override fun bindPresenter(): MarketingPresenter? {
        return MarketingPresenter(this, this)
    }

    private var mDish: String? = null
    private var mAction: String = ACTION_DISCOUNT_ADD
    private var mDiscountType: Int? = -1
    private var mDiscountDishEditAdapter: DiscountDishEditAdapter? = null
    private var mList = mutableListOf<String>()
    override fun initial() {
        mAction = intent.getStringExtra(PARAM1)
        mDish = intent.getStringExtra(PARAM2)
        if (mAction == ACTION_DISCOUNT_ADD) {
            tv_discount_dish_edit_title.text = "添加折扣"
            ly_discount_dish_edit_dishes_rv_title.visibility = View.GONE    // TODO: 2019/1/16 添加成功之后设置为VISIBLE
            btn_discount_dish_edit_delete.visibility = View.GONE
        } else if (mAction == ACTION_DISCOUNT_EDIT) {
            tv_discount_dish_edit_title.text = "编辑折扣"
            ly_discount_dish_edit_dishes_rv_title.visibility = View.VISIBLE
            btn_discount_dish_edit_delete.visibility = View.VISIBLE
            mList = mutableListOf("1", "2", "3")
        }
        btn_discount_dish_edit_add.setOnClickListener {
            DishManageActivity.startThis(this, ACTION_DISCOUNT_DISH_CHOOSE)
        }
        tv_discount_dish_edit_time.setOnClickListener {
            showDatePickDialog(true)
        }
        swi_discount_dish_edit_discount.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                ly_discount_dish_edit_discount.visibility = View.VISIBLE
                mDiscountType = EDIT_DISCOUNT_TYPE1
                v_discount_dish_edit_discount1.isSelected = true
                v_discount_dish_edit_discount2.isSelected = false
                v_discount_dish_edit_discount3.isSelected = false
            } else {
                ly_discount_dish_edit_discount.visibility = View.GONE
                mDiscountType = -1
            }
        }
        ly_discount_dish_edit_discount1.setOnClickListener {
            mDiscountType = EDIT_DISCOUNT_TYPE1
            v_discount_dish_edit_discount1.isSelected = true
            v_discount_dish_edit_discount2.isSelected = false
            v_discount_dish_edit_discount3.isSelected = false
        }
        ly_discount_dish_edit_discount2.setOnClickListener {
            mDiscountType = EDIT_DISCOUNT_TYPE2
            v_discount_dish_edit_discount1.isSelected = false
            v_discount_dish_edit_discount2.isSelected = true
            v_discount_dish_edit_discount3.isSelected = false
        }
        ly_discount_dish_edit_discount3.setOnClickListener {
            mDiscountType = EDIT_DISCOUNT_TYPE3
            v_discount_dish_edit_discount1.isSelected = false
            v_discount_dish_edit_discount2.isSelected = false
            v_discount_dish_edit_discount3.isSelected = true
        }
        mDiscountDishEditAdapter = DiscountDishEditAdapter(mList, mAction)
        rv_discount_dish_edit_dishes.layoutManager = LinearLayoutManager(this)
        rv_discount_dish_edit_dishes.adapter = mDiscountDishEditAdapter
        btn_discount_dish_edit_save.setOnClickListener {

        }
        btn_discount_dish_edit_delete.setOnClickListener {

        }
    }

    override fun onResume() {
        super.onResume()
        RxBus.getDefault().subscribe(this, RX_DISH, object : RxBus.Callback<DishEntity>() {
            override fun onEvent(t: DishEntity?) {
                mList.add(t!!.dishName)
//                t!!.price
//                t.dishName
                mDiscountDishEditAdapter!!.notifyDataSetChanged()
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        RxBus.getDefault().unregister(this)
    }

    private var mStartTime: String = ""
    private var mEndTime: String = ""
    private fun showDatePickDialog(start: Boolean) {
        val pvTime = TimePickerBuilder(this, OnTimeSelectListener { date, v ->
            if (start) {
                mStartTime = SimpleDateFormat("yyyy-MM-dd").format(date)
                showDatePickDialog(false)
            } else {
                mEndTime = SimpleDateFormat("yyyy-MM-dd").format(date)
                tv_discount_dish_edit_time.text = "$mStartTime 至 $mEndTime"
            }
        }).setTimeSelectChangeListener { Log.i("pvTime", "onTimeSelectChanged") }
                .setType(booleanArrayOf(true, true, true, false, false, false))
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .build()
        val dialog = pvTime.dialog
        if (dialog != null) {
            val params = FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM
                                                 )
            params.leftMargin = 0
            params.rightMargin = 0
            pvTime.dialogContainerLayout.layoutParams = params
            val dialogWindow = dialog.window
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim)//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM)//改成Bottom,底部显示
            }
        }
        dialog.show()
    }

}
